use std::error::Error;
use std::fmt::{Display, Formatter};
use std::{fmt, result};

type Result<T> = result::Result<T, DoubleError>;

#[derive(Debug, Clone)]
struct DoubleError;

impl Display for DoubleError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "invalid first item to double")
    }
}

impl Error for DoubleError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        None
    }
}

fn double_first(vec: Vec<&str>) -> Result<i32> {
    vec.first()
        .ok_or(DoubleError)
        .and_then(|s| s.parse::<i32>().map_err(|_| DoubleError).map(|i| 2 * i))
}

fn print(result: Result<i32>) {
    match result {
        Ok(n) => println!("the first doubleed is {}", n),
        Err(e) => println!("Error: {}", e),
    }
}

fn main() {
    println!("Hello, world!");
    let numbers = vec!["42", "93", "18"];
    let empty = vec![];
    let strings = vec!["tofu", "93", "18"];

    print(double_first(numbers));
    print(double_first(empty));
    print(double_first(strings));
}
